package net.html;

import java.io.IOException;
import java.io.Writer;

import javax.swing.text.MutableAttributeSet;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.HTML.Tag;

public class LineBreakingTagStripper extends HTMLEditorKit.ParserCallback
{
	private Writer out;
	private String lingSeperator;
	
	public LineBreakingTagStripper(Writer out)
	{
		
	}
	public LineBreakingTagStripper(Writer out,String lineSeparator)
	{
		this.out = out;
		this.lingSeperator = lineSeparator;
	}
	@Override
	public void handleText(char[] data, int pos)
	{
		try
		{
			out.write(data);
			out.flush();
		}
		catch (Exception e)
		{
		}
	}
	@Override
	public void handleEndTag(Tag t, int pos)
	{
		if(t.isBlock())
		{
			try
			{
				out.write(lingSeperator);
				out.write(lingSeperator);
			}
			catch (IOException e)
			{
				e.printStackTrace();
			}
		}else if(t.breaksFlow())
		{
			try
			{
				out.write(lingSeperator);
			}
			catch (IOException e)
			{
				e.printStackTrace();
			}
		}
	}
	@Override
	public void handleSimpleTag(Tag t, MutableAttributeSet a, int pos)
	{
		try
		{
			if(t.isBlock())
			{
				out.write(lingSeperator);
				out.write(lingSeperator);
			}else if(t.breaksFlow())
			{
				out.write(lingSeperator);
			}else
			{
				out.write(' ');
			}
		}
		catch (Exception e)
		{
		}
	}
}
